package com.lhn.bbs.controller;

import com.lhn.bbs.dto.ActionResult;
import com.lhn.bbs.entity.User;
import com.lhn.bbs.service.UserService;
import com.lhn.bbs.utils.QiniuUpload;
import com.lhn.bbs.vo.EditUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


/**
 * @ Author :lihainan
 * @ ClassNmae :UserController
 * @ Description :TODO
 * @ Date :2021/3/31
 * @ Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Autowired(required = false)
    private UserService userService;

    @RequestMapping(value="/allusers",method=RequestMethod.GET)
    public ActionResult allusers(@RequestParam(value="page",defaultValue="1")Integer page,
                                 @RequestParam(value = "limit", defaultValue = "3") Integer limit,
                                 @RequestParam(value = "status") Integer status){

        log.info("---------"+page+"===="+limit);

        return userService.findUserAll(page,limit,status);

    }

    /**
     * 根据账号删除用户
     * @param sno
     * @return
     */
    @RequestMapping(value="/deleteuser",method=RequestMethod.GET)
    public ActionResult deleteuser(@RequestParam(value="sno")String sno){

        log.info("---------"+sno+"====");

        return userService.deleteuserBysno(sno);

    }
//    修改密码
   @RequestMapping(value="/editpassword",method=RequestMethod.GET)
    public ActionResult deleteuser(@RequestParam(value="sno")String sno,
                                   @RequestParam(value="password")String password){

        log.info("---------"+sno+"====");

        return userService.editpassword(sno,password);

    }
//    管理员修改跟人信息
    @RequestMapping(value="/edituser",method=RequestMethod.POST)
    public ActionResult edituser(@RequestBody EditUser editUser){

        log.info("---------"+editUser.getSno()+"====");

        return userService.editUserBysno(editUser);

    }
    //   个人修改跟人信息
    @RequestMapping(value="/oneselfedit",method=RequestMethod.GET)
    public ActionResult oneselfedit(@RequestParam(value="sno")String sno,
                                    @RequestParam(value="username")String username,
                                    @RequestParam(value="sex")String sex,
                                    @RequestParam(value="sign")String sign,
                                    @RequestParam(value="email")String email,
                                    @RequestParam(value="sdept")String sdept){

        log.info("---------"+sno+"====");

        return userService.editUserByid(sno,username,sex,email,sdept,sign);

    }
//     图片上传
    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public ActionResult upload(MultipartFile file) throws IOException {
        log.info("++++++++"+file);
        String fileUrl = QiniuUpload.uploadFile(file);
        if(fileUrl!=null){
            String url=fileUrl;
            log.info("++++++++"+url);
            return new ActionResult(200,"上传成功",url,null);
        }
        return new ActionResult(400,"上传失败",null,null);
    }

}
